package com.dlc.felear.xiaoensale.activity;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.DebugItemAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.deviceUtil.Serialhelper;
import com.dlc.felear.xiaoensale.dlc_serial_port.DlcSerialPortClient;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.factory.CmdList;
import com.dlc.felear.xiaoensale.utils.HexUtil;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DeviceDebugActivity extends BaseActivity {
    private static final String TAG = "CheckDeviceActivity";

    @BindView(R.id.tv_debug_show)
    TextView tv;
    @BindView(R.id.gv)
    GridView gridView;
    private DebugItemAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_device_debug;
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter = new DebugItemAdapter(this);
        gridView.setAdapter(adapter);
        adapter.setItems(getdata());
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                handleItem(position);
            }
        });
    }

    private void handleItem(int position) {
        showLongToast("item:" + position);
        ActionStepItem actionStepItem = adapter.getItem(position);
        switch (actionStepItem.getName()) {
            case "握手":
                sendDataToSerial(CmdList.connect());
                break;
            case "蓝牙":
                sendDataToSerial(CmdList.bluetooth());
                break;
            case "红外1":
                sendDataToSerial(CmdList.sendIrData1((byte)0x01,(byte)0x02,(byte)0xff));
                break;
            case "红外2":
                sendDataToSerial(CmdList.sendIrData1((byte)0x02,(byte)0x02,(byte)0xff));
                break;
        }
    }

    private List<ActionStepItem> getdata() {
//        int type, String name, int pic,int serialNo
        List<ActionStepItem> data = new ArrayList<>();
        data.add(new ActionStepItem(0, "握手", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "蓝牙", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外1", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外2", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外3", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外4", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外5", R.drawable.app_logo, 1));
        data.add(new ActionStepItem(0, "红外6", R.drawable.app_logo, 1));
        return data;
    }

    private void sendDataToSerial(byte[] data) {
        showTv(HexUtil.bytes2HexString(data));
        DlcSerialPortClient.getInstance().send(Serialhelper.getInstance().getKzbSerialNo(), data);
    }

    private void showTv(String msg) {
        tv.setText(msg);
    }

    @Subscriber(tag = Constant.EVENT_RECEIVE_INPU_IO)
    public void receiverInputIo(final byte[] orders) {
        Log.e(TAG, "EVENT_RECEIVE_INPU_IO111:" + HexUtil.bytes2HexString(orders));
//        if (CmdList.checkDataComplete(orders) && orders[0] == (byte) 0xA5) {
//            if (orders[1] == (byte) 0x10) {
//                showTv(HexUtil.hexToAscii(orders,3,orders.length-2));
//            } else if (orders[1] == (byte) 0x12) {
//                showTv(HexUtil.hexToAscii(orders,3,orders.length-2));
//            }
//        } else {
//        }
    }


}